package approval.dao;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import resource.pojo.ListApprove;


import models.util.MySqlDataAccessHelper;

public class ListApproveDAO {
	public static boolean insert(ArrayList<ListApprove> lst){
		int a = 0;
		MySqlDataAccessHelper mysql = new MySqlDataAccessHelper();		
		String sql = "call sp_insertListApprove (?,?,?,?,?)";
		CallableStatement cs = null;
		try {
			mysql.open();
			int or = 1;
			for (ListApprove l : lst) {
				cs = mysql.getConnection().prepareCall(sql);
				cs.setString(1, l.getiDGroupApprove());
				cs.setString(2, l.getiDEmployee());
				cs.setInt(3, 1);
				cs.setInt(4, or++);
				cs.setInt(5, or==2?2:4);
				a += cs.executeUpdate();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			mysql.close();
			try {
				cs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return a == lst.size();
	}
	
	public static boolean insertNoOrder(ArrayList<ListApprove> lst){
		int a = 0;
		MySqlDataAccessHelper mysql = new MySqlDataAccessHelper();			
		try {
			mysql.open();
			for (ListApprove l : lst) {
				String sql = String.format("INSERT INTO listapprove(IDGroupApprove,IDEmployee,IDtypeApprove,listapprove.Order,IDStatus) VALUES('%s','%s',%s,%s,%s)",l.getiDGroupApprove(),l.getiDEmployee(),2,1,2);
				a += mysql.executeUpdate(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			mysql.close();
		}
		return a == lst.size();
	}
	
	public static boolean insertReference(ArrayList<ListApprove> lst){
		int a = 0;
		MySqlDataAccessHelper mysql = new MySqlDataAccessHelper();			
		try {
			mysql.open();
			for (ListApprove l : lst) {
				String sql = String.format("INSERT INTO listapprove(IDGroupApprove,IDEmployee,IDtypeApprove,listapprove.Order,IDStatus) VALUES('%s','%s',%s,%s,%s)",l.getiDGroupApprove(),l.getiDEmployee(),2,1,5);
				a += mysql.executeUpdate(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			mysql.close();
		}
		return a == lst.size();
	}
}


